package com.alibaba.citrus.cr.mn.order.center.facade.repository.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.ForecastOrderLineIndexToForecastOrderLineSDOConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.ForecastOrderLineRepoQueryReqToSearchForecastOrderRequestConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.ForecastOrderRepositoryConverter;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.ForecastOrderLineQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crforecastsales.domian.forecastorderline.dto.BatchLoadListRequest;
import com.epoch.app.crforecastsales.domian.forecastorderline.dto.BatchLoadListResponse;
import com.epoch.app.crforecastsales.domian.forecastorderline.dto.BatchQueryByBizDoListResponse;
import com.epoch.app.crforecastsales.domian.forecastorderline.dto.LoadForecastOrderLineRequest;
import com.epoch.app.crforecastsales.domian.forecastorderline.model.ForecastOrderLine;
import com.epoch.app.crforecastsales.domian.forecastorderline.service.ForecastOrderLineService;
import com.epoch.app.crforecastsales.forecastorderlineindex.model.ForecastOrderLineIndex;
import com.epoch.app.crforecastsales.forecastorderlineindex.service.ForecastOrderLineIndexService;
import com.epoch.app.crforecastsales.model.dto.ForecastOrderLineBizDO;
import com.epoch.app.crforecastsales.model.dto.SearchForecastOrderRequest;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderLineRepoQueryReq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("forecastOrderLineQueryRepositoryBiz")
public class ForecastOrderLineQueryRepositoryImpl implements ForecastOrderLineQueryRepository {
    private static final Log log = Log.getLogger(ForecastOrderLineQueryRepositoryImpl.class);
    @Resource
    private ForecastOrderLineService forecastOrderLineService;
    @Resource
    private ForecastOrderLineIndexService forecastOrderLineIndexService;

    @Override
    public ForecastOrderLineSDO query(String id) {
        LoadForecastOrderLineRequest loadForecastOrderLineRequest = new LoadForecastOrderLineRequest();
        loadForecastOrderLineRequest.setId(id);
        ForecastOrderLine forecastOrderLine = forecastOrderLineService.loadForecastOrderLine(loadForecastOrderLineRequest);
        ForecastOrderLineSDO forecastOrderLineSDO = ForecastOrderRepositoryConverter.toForecastOrderLineSDO(forecastOrderLine);
        return forecastOrderLineSDO;
    }

    @Override
    public ForecastOrderSDO queryByOrderId(ForecastOrderSDO forecastOrderSDO) {
        ForecastOrderLine forecastOrderLineLoadListRequest = new ForecastOrderLine();
        forecastOrderLineLoadListRequest.setMainOrderId(forecastOrderSDO.getId());
        Result<List<ForecastOrderLine>> response = forecastOrderLineService.loadForecastOrderLineList(forecastOrderLineLoadListRequest);
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = Nullable.stream(response.getResult()).
                map(ForecastOrderRepositoryConverter::toForecastOrderLineSDO).collect(Collectors.toList());
        forecastOrderSDO.setForecastOrderLineSDOList(forecastOrderLineSDOList);
        return forecastOrderSDO;
    }

    @Override
    public PageResult<List<ForecastOrderLineSDO>> batchLoadList(List<String> ids, List<String> mainOrderIds ) {
        BatchLoadListRequest forecastOrderLineLoadListRequest = new BatchLoadListRequest();
        forecastOrderLineLoadListRequest.setIds(ids);
        forecastOrderLineLoadListRequest.setMainOrderIds(mainOrderIds);
        BatchLoadListResponse batchLoadListResponse = forecastOrderLineService.batchLoadList(forecastOrderLineLoadListRequest);
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = Nullable.stream(batchLoadListResponse.getResult()).
                map(ForecastOrderRepositoryConverter::toForecastOrderLineSDO).collect(Collectors.toList());
        return PageResult.of(forecastOrderLineSDOList, batchLoadListResponse.getResult().size());
    }

    @Override
    public Result<List<com.epoch.app.mnordercenter.model.dto.ForecastOrderLineSDO>> pageQueryFromEs(ForecastOrderLineRepoQueryReq forecastOrderLineRepoQueryReq) {
        SearchForecastOrderRequest searchForecastOrderRequest = ForecastOrderLineRepoQueryReqToSearchForecastOrderRequestConvertor.convert(forecastOrderLineRepoQueryReq);
        Result<List<ForecastOrderLineIndex>> response = forecastOrderLineIndexService.queryForecastOrderLineByEs(searchForecastOrderRequest);
        List<com.epoch.app.mnordercenter.model.dto.ForecastOrderLineSDO> result = Nullable.stream(response.getResult()).
                map(ForecastOrderLineIndexToForecastOrderLineSDOConvertor::convert).collect(Collectors.toList());
        return Result.listSuccess(result,response.getTotal());
    }

    @Override
    public BatchQueryByBizDoListResponse queryByBizList(ForecastOrderLineBizDO orderLineBizDO) {

        return forecastOrderLineService.batchQueryByBizDoList(orderLineBizDO);
    }
}
